﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;

namespace SGS.Cards
{
    public static class NameMapping
    {
        private static XmlDocument _document = null;
        private static XmlNamespaceManager nsmgr = null;

        static NameMapping()
        {
            _document = new XmlDocument();
            _document.LoadXml( DataConstructor.ReadResourceXml( "SGS.Resource.CardNameMapping.xml" ) );

            nsmgr = new XmlNamespaceManager( _document.NameTable );
            nsmgr.AddNamespace( "mns", "http://tempuri.org/CardName.xsd" );
        }

        public static string Parse( CardType type )
        {
            string xpath = string.Format("mns:NameMapping//mns:CardName[@Type='{0}']", type);
            var node = _document.SelectSingleNode( xpath, nsmgr );

            if (node == null)
            {
                return type.ToString();
            }
            else
            {
                return node.Attributes["Name"].Value;
            }
        }

        public static string ParseEquipment( CardType type, string className )
        {
            string xpath = string.Format( "mns:NameMapping//mns:EquipmentName[@Type='{0}' and @ClassName='{1}']", type, className );
            var node = _document.SelectSingleNode( xpath, nsmgr );

            if (node == null)
            {
                return type.ToString();
            }
            else
            {
                return node.Attributes["Name"].Value;
            }
        }
    }
}
